package hate.util;


import com.lowagie.text.*;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.rtf.RtfWriter2;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;

public class WordImg {

    public static void main (String args[]){
        try {
            exportDoc();
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    /**
     *
     * @Description: 将网页内容导出为word
     * @param @param file
     * @param @throws DocumentException
     * @param @throws IOException 设定文件
     * @return void 返回类型
     * @throws
     */
    public static String exportDoc() throws DocumentException, IOException {

        // 设置纸张大小

        Document document = new Document(PageSize.A4);

        // 建立一个书写器(Writer)与document对象关联，通过书写器(Writer)可以将文档写入到磁盘中
        // ByteArrayOutputStream baos = new ByteArrayOutputStream();

        File file = new File("C:\\Users\\13918\\Desktop\\img\\report.doc");

        RtfWriter2.getInstance(document, new FileOutputStream(file));

        document.open();

        // 设置中文字体

        BaseFont bfChinese = BaseFont.createFont(BaseFont.HELVETICA,
                BaseFont.WINANSI, BaseFont.NOT_EMBEDDED);
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("0", "0");
	    map.put("1", "1");
	    map.put("2", "2");
        map.put("3", "3");
        map.put("4", "4");
        map.put("5", "5");
        map.put("6", "6");
        map.put("7", "7");
        map.put("8", "8");
        map.put("9", "9");
        map.put("10", "10");
        Font fontChinese = new Font(bfChinese, 10.5F, Font.NORMAL);// 五号
        Paragraph ret = new  Paragraph("附表1： 基线按ATA章节分类情况统计表", fontChinese);
        PdfPTable tableBox = new PdfPTable(3);

        document.close();

        // 得到输入流
        // wordFile = new ByteArrayInputStream(baos.toByteArray());
        // baos.close();
        return "";
    }

    /**
     * 生成pdf表格
     * @return
     * @see
     */
    private static Paragraph generatePdfATATable() throws Exception {
        BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
        Font fontChinese = new Font(bfChinese, 10.5F, Font.NORMAL);// 五号
        Paragraph ret = new Paragraph("附表1： 基线按ATA章节分类情况统计表", fontChinese);
        PdfPTable tableBox = new PdfPTable(3);
        tableBox.setWidths(new float[] { 0.3f, 0.4f, 0.3f });// 每个单元格占多宽
        tableBox.setWidthPercentage(80f);
        // 获取ATA分类的结果集
        //List<BaselineATA> ataList = countFileOrDMByATA(baselineData);
        // 创建表格格式及内容
        tableBox.addCell(getCell(new Phrase("基线按ATA章节分类情况", fontChinese), false, 3, 1));
        tableBox.addCell(getCell(new Phrase("ATA", fontChinese), false, 1, 1));
        tableBox.addCell(getCell(new Phrase("文件/图样数量", fontChinese), false, 1, 1));
        tableBox.addCell(getCell(new Phrase("DM数量", fontChinese), false, 1, 1));
        // 遍历查询出的结果
        //for (BaselineATA ata : ataList) {
            tableBox.addCell(getCell(new Phrase("11", fontChinese), false, 1, 1));
            tableBox.addCell(getCell(new Phrase(String.valueOf("22"), fontChinese), false, 1, 1));
            tableBox.addCell(getCell(new Phrase(String.valueOf("33"), fontChinese), false, 1, 1));
       // }
        ret.add(tableBox);
        return ret;

    }

//每个ｃｅｌｌ的格式，合并单元格情况
    private static PdfPCell getCell(Phrase phrase, boolean yellowFlag, int colSpan, int rowSpan) {
        PdfPCell cells = new PdfPCell(phrase);
        cells.setUseAscender(true);
        cells.setMinimumHeight(20f);
        cells.setHorizontalAlignment(1);
        cells.setVerticalAlignment(5);
        cells.setColspan(colSpan);
        //cells.setRowspan(rowSpan);
        cells.setNoWrap(false);
        return cells;
    }
}
